function debounce(fn = new Function(), delay: number) {
    let flag: any = null
    return function (...args: any) {
        if (flag) {
            clearTimeout(flag);
            flag = null;
            console.log("防抖中");
        }


        flag = setTimeout(() => {
            fn.apply(null, args);
            flag = null;
        }, delay)
    }
}



export default {
    mounted(el = new Element(), binding: any, vnode: any) {
        const {
            arg: eventType,
            value: { handler, delay },
        } = binding;

        vnode.eventType = eventType;
        vnode.eventHandler = debounce(handler, delay);

        el.addEventListener(vnode.eventType, vnode.eventHandler);
    },
    beforeUnmount(el = new Element(), binding: any, vnode: any) {
        el.removeEventListener(vnode.eventType, vnode.eventHandler);
    }
}